/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.kedll.library.adapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 多选
 *
 * @author QiQi
 * @date 2017/5/27
 */

public final class MultiSelectAdapter<T> extends MultiTypeAdapter {
    private List<T> multiSelectionList;
    private int selection;
    private boolean isMulti;

    public MultiSelectAdapter(boolean isMulti) {
        this.isMulti = isMulti;
        this.multiSelectionList = new ArrayList<>(isMulti ? 16 : 1);
        this.selection = -1;
    }

    public final int getSelection() {
        return this.selection;
    }

    public final void setSelection(int selection) {
        this.selection = selection;
    }

    public final boolean toggle(T data) {
        if (this.isMulti) {
            this.multiSelection(data);
        } else {
            this.singleSelection(data);
        }

        return true;
    }

    private final void multiSelection(T data) {
        if (!this.multiSelectionList.contains(data)) {
            this.multiSelectionList.add(data);
        } else {
            this.multiSelectionList.remove(data);
        }
    }

    private final void singleSelection(T data) {
        if (this.multiSelectionList.contains(data)) {
            this.multiSelectionList.remove(data);
        } else {
            this.clearSelectionList();
            this.multiSelectionList.add(data);
        }
    }

    public final void clearSelectionList() {
        this.multiSelectionList.clear();
    }

    @NotNull
    public final List<T> getMultiSelectionList() {
        return this.multiSelectionList;
    }

    public final void setMultiSelectionList(@NotNull List<T> multiSelectionList) {
        this.multiSelectionList = multiSelectionList;
    }

    public final boolean getMulti() {
        return this.isMulti;
    }

    public final void setMulti(boolean multi) {
        this.isMulti = multi;
    }
}

